package com.woniu.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.woniu.entity.*;
import com.woniu.service.FinalTrialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@Api(value = "订单模块终审控制器", tags = {"终审"})
@RestController
@RequestMapping("/finalTrial")
public class FinalTrialController extends BaseController {
    @Autowired
    FinalTrialService finalTrialService;


    @ApiOperation(value = "查询所有需要终审订单",notes = "根据订单状态查询所有需要终审的订单",
            response=FinalTrialClientInfoVO.class, responseContainer="List", httpMethod="GET",
            produces="application/json",code=200,tags = {"终审"})
    @GetMapping("/listAllApplicant")
    public TableDataInfo listAllApplicant()
    {
        startPage();
        List<FinalTrialClientInfoVO> list = finalTrialService.listAllApplicant();
        return getDataTable(list);
    }


    @ApiOperation(value = "查询终审订单详情信息",notes = "根据订单状态查询所有需要终审的订单",
            response=FinalTrialClientInfoVO.class, responseContainer="List", httpMethod="GET",
            produces="application/json",code=200,tags = {"终审"})
    @ApiImplicitParams(@ApiImplicitParam(name = "clientId",value="客户id",required=true,dataTypeClass=Integer.class,paramType="path"))
    @GetMapping("/getDetailInfo/{clientId}")
    public AjaxResult getDetailInfo(@PathVariable("clientId") Integer clientId)
    {
        List<FinalTrialDetailVO> list = finalTrialService.getDetailInfo(clientId);
        System.out.println(list);
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",list);
    }


    @ApiOperation(value = "获取字典标签",notes = "根据字典类型、字典键值查询字典标签名称",
            response=String.class, httpMethod="GET",
            produces="application/json",code=200,tags = {"终审"})
    @ApiImplicitParams(
            value={
                    @ApiImplicitParam(name = "applicantStatus",value="订单申请状态码",required=true,dataTypeClass=Integer.class,paramType="path"),
                    @ApiImplicitParam(name = "dictType",value="字典类型",required=true,dataTypeClass=String.class,paramType="path")
            })
    @GetMapping("/getDetailInfo/{dictType}/{applicantStatus}")
    public AjaxResult getApplicantStatus(@PathVariable("applicantStatus") Integer applicantStatus,@PathVariable("dictType") String dictType)
    {
        String dictLabel = DictUtils.getDictLabel(dictType, String.valueOf(applicantStatus));
        return new AjaxResult(HttpStatus.SUCCESS,"字典标签值查询成功",dictLabel);
    }



    @ApiOperation(value = "查询历史进件",notes = "根据用户id查询所有历史订单",
            response=HistoryEntity.class, responseContainer="List", httpMethod="GET",
            produces="application/json",code=200,tags = {"终审"})
    @ApiImplicitParams(@ApiImplicitParam(name = "clientId",value="客户id",required=true,dataTypeClass=Integer.class,paramType="path"))
    @GetMapping("/getHistoryList/{clientId}")
    public AjaxResult getHistoryList(@PathVariable("clientId") Integer clientId)
    {
        List<HistoryEntity> list = finalTrialService.getHistoryList(clientId);
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",list);
    }


    @ApiOperation(value = "更新终审订单",notes = "更新终审订单状态",
            response=AjaxResult.class, httpMethod="POST",
            produces="application/json",code=200,tags = {"终审"})
    @ApiImplicitParams(@ApiImplicitParam(name = "params",value="放款审批需要的参数",required=true,dataTypeClass=Map.class,paramType="body"))
    @ResponseBody
    @PostMapping("/updateFinalTrialOrder")
    public AjaxResult updateFinalTrialOrder(@RequestBody Map<String,Object> params)
    {
        System.out.println(params);
        Integer res = finalTrialService.updateFinalTrialOrder(params);
        if (res>0){
            return new AjaxResult(HttpStatus.SUCCESS,"终审通过成功",res);
        }else{
            return new AjaxResult(HttpStatus.ERROR,"终审通过失败",null);
        }
    }


    @ApiOperation(value = "查询终审进件信息",notes = "根据用户输入复合条件查询所有终审订单",
            response=AjaxResult.class, responseContainer="List", httpMethod="POST",
            produces="application/json",code=200,tags = {"终审"})
    @ApiImplicitParams(@ApiImplicitParam(name = "params",value="查询需要的参数",required=true,dataTypeClass=Map.class,paramType="body"))
    @ResponseBody
    @PostMapping("/searchClientProduction")
    public AjaxResult searchClientProduction(@RequestBody Map<String,Object> params)
    {
        System.out.println(params);
        List<FinalTrialClientInfoVO> res = finalTrialService.searchClientProduction(params);
        System.out.println("查询结果"+res);
        if (res.size()>0){
            return new AjaxResult(HttpStatus.SUCCESS,"查询成功",res);
        }else{
            return new AjaxResult(HttpStatus.ERROR,"暂无数据",null);
        }
    }


    @ApiOperation(value = "导出终审进件信息",notes = "根据用户选择的对应行导出所有终审订单信息",
            response=void.class, httpMethod="POST",
            produces="application/json",code=200,tags = {"终审"})
    @PostMapping("/exportChosen")
    @ResponseBody
    public void export(HttpServletResponse response, UsualExportVO data){
        System.out.println(data);
        List<FinalTrialExportVO> list = finalTrialService.selectFinalTrialOrders(data);
        ExcelUtil<FinalTrialExportVO> util = new ExcelUtil<> (FinalTrialExportVO.class);
        util.exportExcel(response,list,KeyWords.EXPORT_FINAL_TRIAL_NAME);
    }
}
